package gateway

import (
	"context"
	"fmt"
	"net/http"
)

// 服务接口与实现

// Service 后端服务接口
type Service interface {
	HandleRequest(ctx *context.Context, req *http.Request) (*http.Response, error)
	Name() string
}

// UserService 具体服务
type UserService struct {
	endpoint string
}

// NewUserService
func NewUserService(endpoint string) *UserService {
	return &UserService{
		endpoint: endpoint,
	}
}

func (s *UserService) HandleRequest(ctx *context.Context, req *http.Request) (*http.Response, error) {
	fmt.Printf("%s start handleRequest\n", s.Name())
	resp := &http.Response{
		StatusCode: http.StatusOK,
		Body:       http.NoBody,
	}
	return resp, nil
}

func (s *UserService) Name() string {
	return "user_service"
}

// OrderService
type OrderService struct {
	endpoint string
}

// NewOrderService
func NewOrderService(endpoint string) *OrderService {
	return &OrderService{
		endpoint: endpoint,
	}
}

func (s *OrderService) HandleRequest(ctx *context.Context, req *http.Request) (*http.Response, error) {
	fmt.Printf("%s start handleRequest\n", s.Name())
	resp := &http.Response{
		StatusCode: http.StatusOK,
		Body:       http.NoBody,
	}
	return resp, nil
}

func (s *OrderService) Name() string {
	return "order_service"
}
